﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace REVISIONMAX30.Frame
{
    public enum FrResultSeverityType : int
    {
        None = 0,
        Info = 1,
        Warning = 2,
        Error = 3,
        Critical = 4
    }

    public class FrResultType
    {
        public FrResultSeverityType Severity { get; set; }

        public string Message { get; set; }

        public List<FrResultType> SubResultList { get; set; }

        public FrResultType()
        {
            SubResultList = new List<FrResultType>();
        }

        public FrResultType(string pMessage, FrResultSeverityType pSeverity)
        {
            Severity = pSeverity;
            Message = pMessage;
            SubResultList = new List<FrResultType>();
        }

        public FrResultType(Exception pException)
        {
            Severity = FrResultSeverityType.Critical;
            Message = pException.Message;
            SubResultList = new List<FrResultType>();
        }

        public void AddResult(Exception pException)
        {
            AddResult(pException.Message, FrResultSeverityType.Critical);
        }

        public void AddResult(FrResultType pResult)
        {
            if (Message == null)
            {
                Severity = pResult.Severity;
                Message = pResult.Message;
            }
            else
            {
        
                SubResultList.Add(new FrResultType(pResult.Message, pResult.Severity));
            }
        }

        public void AddResult(string pMessage, FrResultSeverityType pSeverity)
        {
            if (Message == null)
            {

                Severity = pSeverity;
                Message = pMessage;
            }
            else
            {
           
                SubResultList.Add(new FrResultType(pMessage, pSeverity));
            }
        }

        public bool HasErrorOrCritical()
        {
            bool tHasErrorOrCritical = (Severity == FrResultSeverityType.Error || Severity == FrResultSeverityType.Critical);

            if (!tHasErrorOrCritical && SubResultList != null)
            {
                foreach (var tSubResult in SubResultList)
                {
                    tHasErrorOrCritical = tSubResult.HasErrorOrCritical();
                    if (tHasErrorOrCritical) break;
                }
            }

            return tHasErrorOrCritical;

        }
 

        public List<FrResultType> GetFlatList()
        {
            var tList = new List<FrResultType>();

            if (this.Message.HasValue())
            {
                tList.Add(this);

                if (this.SubResultList.Count > 0)
                {
                    tList.AddRange(this.SubResultList);
                }
            }

            return tList;
        }



    }
}
